/**
 * 二分查找
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function (nums, target) {

    let l = 0
    let r = nums.length - 1

    while (l <= r) {
        let mid = Math.round((l + r) / 2)

        if (nums[mid] === target) {
            return mid
        }

        // 如果左边有序
        if (nums[l] < nums[mid]) {
            if (target < nums[mid] && target >= nums[l]) {
                r = mid - 1
            } else {
                l = mid + 1
            }
        } else {
            // 如果右边有序
            if (target > nums[mid] && target <= nums[nums.length - 1]) {
                l = mid + 1
            } else {
                r = mid - 1
            }
        }
    }
    return -1
};

console.log(search([4, 5, 6, 7, 0, 1, 2], 0))